// 3dtile剖切

let tilesetLayer

export function setTilesetLayer (layer){
    tilesetLayer = layer
}

export function getTilesetLayer () {
    return tilesetLayer
}

// 正切
export function clippingType(type) {
    tilesetLayer.planClip.type = mars3d.ClipType[type]
    if (viewer.scene.requestRenderMode) {
        viewer.scene.requestRender();
    }
}

// 距离
export function rangeDistance(value) {
    tilesetLayer.planClip.distance = value
    if (viewer.scene.requestRenderMode) {
        viewer.scene.requestRender();
    }
}
// 偏移量
export function rangeNormalZ(value) {
    tilesetLayer.planClip.normalZ = value
    if (viewer.scene.requestRenderMode) {
        viewer.scene.requestRender();
    }
}

// 线剖切
export function drawLine() {
    tilesetLayer.planClip.clear()
    viewer.graphicLayer.startDraw({
        type: "polyline",
        maxPointNum: 2,
        style: {
            color: "#007be6",
            opacity: 0.8,
            outline: false
        },
        success: function (graphic) {
            // 绘制成功后回调
            const positions = graphic.positionsShow
            viewer.graphicLayer.clear()
            console.log("绘制坐标为", JSON.stringify(mars3d.LngLatArray.toArray(positions))) // 方便测试拷贝坐标
            tilesetLayer.planClip.positions = positions
        }
    })
}

// 绘制矩形
export function drawExtent() {
    tilesetLayer.planClip.clear()

    viewer.graphicLayer.startDraw({
        type: "rectangle",
        style: {
            color: "#007be6",
            opacity: 0.8,
            outline: false
        },
        success: function (graphic) {
            // 绘制成功后回调
            const positions = graphic.getOutlinePositions(false)
            viewer.graphicLayer.clear()
            console.log("绘制坐标为", JSON.stringify(mars3d.LngLatArray.toArray(positions))) // 方便测试拷贝坐标

            tilesetLayer.planClip.positions = positions
        }
    })
}

// 绘制面
export function drawPoly() {
    tilesetLayer.planClip.clear()

    viewer.graphicLayer.startDraw({
        type: "polygon",
        style: {
            color: "#007be6",
            opacity: 0.5,
            clampToGround: true
        },
        success: function (graphic) {
            // 绘制成功后回调
            const positions = graphic.positionsShow
            viewer.graphicLayer.clear()
            console.log("绘制坐标为", JSON.stringify(mars3d.LngLatArray.toArray(positions))) // 方便测试拷贝坐标

            tilesetLayer.planClip.positions = positions
        }
    })
}

// 绘制面(外切)
export function drawPoly2() {
    tilesetLayer.planClip.clear()
    viewer.graphicLayer.startDraw({
        type: "polygon",
        style: {
            color: "#007be6",
            opacity: 0.5,
            clampToGround: true
        },
        success: function (graphic) {
            // 绘制成功后回调
            const positions = graphic.positionsShow
            viewer.graphicLayer.clear()
            console.log("绘制坐标为", JSON.stringify(mars3d.LngLatArray.toArray(positions))) // 方便测试拷贝坐标

            tilesetLayer.planClip.clipOutSide = true
            tilesetLayer.planClip.positions = positions
        }
    })
}

export function clear() {
    tilesetLayer.planClip.clear()
}